#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/6/12 12:54
# @Author  : TangZhongZheng
# @File    : deal_with.py
# @Description :
import math
import warnings
import pandas as pd

from pandas import DataFrame

from config import *

warnings.filterwarnings("ignore")

pd.options.mode.chained_assignment = None


def del_blank_row(data, aim):
    """
    给特定列去除空行
    :param data:
    :param aim:
    :return:
    """
    for index, row in data.iterrows():
        if isinstance(row[aim], float):
            if math.isnan(row[aim]):
                continue
        text = row[aim]
        # 假设 text 是一个包含空行的字符串
        lines = text.splitlines(True)  # 保留换行符
        # 过滤空行
        lines = [line for line in lines if line.strip()]
        # 重新拼接成字符串
        text = "".join(lines)
        data.loc[index, aim] = text


def deal_with_blank_row(data):
    """
    处理空行
    :param data:
    :return:
    """
    del_blank_row(data, member_position)
    del_blank_row(data, member_otherinfo)
    return data


def deal_with__phone(data):
    """
    处理电话
    :param data:处理前的数据
    :return: data:处理后的数据
    """
    data[member_phone] = deal_with_symbol(data[member_phone])
    data[member_phone] = data[member_phone].str.replace(" ", "")
    data[member_phone] = data[member_phone].str.replace("-", "")
    return data


def deal_with__address(data):
    """
    处理地址
    :param data:
    :return:
    """
    data[member_address] = deal_with_symbol(data[member_address])
    return data


def deal_with__position(data):
    """
    处理位置
    :param data:
    :return:
    """
    data[member_position] = deal_with_symbol(data[member_position])
    return data


def deal_with__education(data):
    """
    处理教育经历
    :param data:
    :return:
    """
    data[member_education] = deal_with_symbol(data[member_education])
    return data


def deal_with_symbol(data):
    """
    去除特殊符号
    :param data:
    :return:
    """
    data = data.str.replace("&", "")
    data = data.str.replace("$", "")
    data = data.str.replace(" @@", ",")
    data = data.str.rstrip()
    return data


def deal_with_gender(data):
    """
    处理性别
    :param data:处理前的数据
    :return data:处理后的数据
    """
    data[member_gender][data[member_gender] == '男'] = '1'
    data[member_gender][data[member_gender] == 'male'] = '1'
    data[member_gender][data[member_gender] == '女'] = '0'
    data[member_gender][data[member_gender] == 'female'] = '0'
    return data


def deal_with_none(data):
    """
    处理空数据
    :param data:
    :return:
    """
    data.fillna(value='', inplace=True)
    return data


def deal_with_date(data):
    """
    处理日期
    :param data:
    :return:
    """
    data[member_birth] = pd.to_datetime(data[member_birth], errors='ignore')
    return data


def deal_with(data: DataFrame, file):
    """
    处理数据的流程，如果需要则可以加入流程，按照deal_with_XXX来命名
    :param data:
    :param i:
    :return:
    """
    data = deal_with_blank_row(data)
    data = deal_with_none(data)
    data = deal_with_gender(data)
    data = deal_with_date(data)
    data = deal_with__phone(data)
    data = deal_with__address(data)
    data = deal_with__position(data)
    data = deal_with__education(data)
    data.to_excel(out + "\\" + file + ".xlsx")
    return data
